package com.zeedev.flyingsaucer.data;

import android.text.TextUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Command implements SQLFormattable, Serializable {
	
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// Constants
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX	
	
	public enum Operator {
		EQUAL, GREATER, LESS, NOT_EQUAL
	}
	
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// Variables  / State
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX	
	
	String property;
	String propertyValue;
	Operator operator;
	Map<String, String> requiredColumns;
	
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// Constructors/Constants
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	
	//DEFAULT IS TO ESCAPE THE VALUES
	public Command(String property, String propertyValue, Operator operator) {
		this(property, propertyValue, operator, true );
	}
	
	public Command(String property, String propertyValue, Operator operator, Boolean escaped) {
		this();
		this.property = property;
		if (escaped) {
			this.setPropertyValueEscape(propertyValue);
		} else {
			this.setPropertyValue(propertyValue);
		}
		this.operator = operator;
		this.addColumn(property);
	}
	
	@Deprecated
	public Command(String property, String propertyValue, Operator operator,
			String[] requiredColumnsIn, Boolean escaped) {
		
		this(property, propertyValue, operator, escaped );
		for (String cur: requiredColumnsIn) {
			addColumn(cur);
		}
	}

	public Command() {
		super();
		requiredColumns = new HashMap<String, String>();
	}

	public Command(Command cin) {
		this();
		this.property = cin.property;
		this.setPropertyValue(cin.propertyValue);
		this.operator = cin.operator;
		for (String curKey : cin.requiredColumns.keySet()) {
			this.requiredColumns.put(curKey, cin.requiredColumns.get(curKey));
		}
	}
	
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// Business Logic
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	@Override
	public Boolean isComplete() {
		Boolean ret = true;
		if (TextUtils.isEmpty(this.property)) {
			ret = false;
		} else if (TextUtils.isEmpty(this.propertyValue)) {
			ret = false;
		} else if (this.operator == null) {
			ret = false;
		}
		return ret;
	}

	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// Getters / Setters
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	
	public String addColumn(String key, String value) {
		return requiredColumns.put(key, value);
	}
	
	public String addColumn(String key) {
		return requiredColumns.put(key, key);
	}

	public String removeColumn(Object key) {
		return requiredColumns.remove(key);
	}
	
	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getPropertyValue() {
		return propertyValue;
	}

	public void setPropertyValue(String propertyValue) {
		this.propertyValue = propertyValue;
	}
	
	public void setPropertyValueEscape(String propertyValue) {
		String newStr = propertyValue.replaceAll("'", "''");
		this.propertyValue = "'" +  newStr +"'";
	}

	public Operator getOperator() {
		return operator;
	}

	public void setOperator(Operator operator) {
		this.operator = operator;
	}
	
	@Override
	public Map<String, String> getColumns() {
		return requiredColumns;
	}

	public void setColumns(Map<String, String> requiredColumns) {
		if (requiredColumns == null) {
			this.requiredColumns = new HashMap<String,String>();
		} else {
			this.requiredColumns = requiredColumns;
		}
		
	}
	
	@Override
	public String getFormattedSQL() {
		return property + getOperatorSQL(this.operator) + this.propertyValue;		
	}
		
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// Utility
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
		
	private static final String getOperatorSQL(Operator opin) {
		switch (opin) {
		case EQUAL:
			return " = ";		
		case NOT_EQUAL:
			return " != ";
		case GREATER:
			return " > ";
		case LESS:
			return " < ";
		default:
			return null;
		}
	}

	





	
	
	
	
	


}
